package com.br.action;

import java.text.MessageFormat;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

/**
 * @author Markus Gebhard
 */
public abstract class AbstractCopyFullyQualifiedClassNameAction implements IActionDelegate {

  private static Clipboard clipboard;
  private IWorkbenchPart activePart;

  protected final void setActivePart(final IWorkbenchPart activePart) {
    this.activePart = activePart;
    
  }

  public final void run(final IAction action) {
    final ISelectedJavaElementProvider provider = new ISelectedJavaElementProvider() {
      public IJavaElement getSelectedJavaElement() throws JavaModelException {
        return getSelectedObject();
      }
    };
    final Shell shell = getShell();
    copyToClipboard(shell, activePart, provider);
  }

  public final static void copyToClipboard(
      final Shell shell,
      final IWorkbenchPart activeWorkbenchPart,
      final ISelectedJavaElementProvider provider) {
    String fullyQualifiedClassName;
    try {
      final IJavaElement selectedObject = provider.getSelectedJavaElement();
      fullyQualifiedClassName = CopyFullyQualifiedClassNameCore.getFullyQualifiedClassName(selectedObject);
    }
    catch (final JavaModelException e) {
      e.printStackTrace();
      return;
    }
    if (fullyQualifiedClassName == null) {
      
    	
    	System.out.println("erro");
      return;
    }

    writeStatusLine(activeWorkbenchPart, fullyQualifiedClassName);
    copyToClipboard(fullyQualifiedClassName, shell);
  }

  private static void writeStatusLine(
      final IWorkbenchPart activeWorkbenchPart,
      final String fullyQualifiedClassName) {
    //final String pattern = Messages.getString("CopyFullyQualifiedClassNameAction.status.copyconfirm.message"); //$NON-NLS-1$
    //final String message = MessageFormat.format(pattern, fullyQualifiedClassName);
    //JavaWorkspaceUtilities.writeStatusMessage(activeWorkbenchPart, message);
  }

  private static void copyToClipboard(final String text, final Shell shell) {
    try {
      getClipboard(shell).setContents(new Object[]{ text }, new Transfer[]{ TextTransfer.getInstance() });
    }
    catch (final SWTError e) {
      if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
        throw e;
      }
     
        copyToClipboard(text, shell);
      
    }
  }

  private synchronized static Clipboard getClipboard(final Shell shell) {
    if (clipboard == null) {
      clipboard = new Clipboard(shell.getDisplay());
    }
    return clipboard;
  }

  protected abstract IJavaElement getSelectedObject() throws JavaModelException;

  protected final Shell getShell() {
    return activePart.getSite().getShell();
  }
}
